﻿using AOP;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

namespace UnitTestProject1
{
    [AOPBefore("UnitTestProject1.AopTestClass", "Before")]
    [AOPAfter("UnitTestProject1.AopTestClass", "After")]
    public class AopTestClass : AOPContext
    {
        [AOPBefore("UnitTestProject1.AopTestClass", "Before")]
        [AOPAfter("UnitTestProject1.AopTestClass", "After")]
        public int TestMethod1(int a, int b)
        {
             Debug.Print("Process Test 1 ：" + a + "\t" + b);
            return a + b;
        }

        public static void Before(ref object obj,ref object a, ref int b)
        {
            List<string> vs = new List<string>();
            // 获取调用堆栈信息
            StackTrace stackTrace = new StackTrace();

            // 遍历调用堆栈中的每个堆栈帧
            foreach (StackFrame frame in stackTrace.GetFrames())
            {
                int v = frame.GetFileLineNumber();
                // 获取堆栈帧的方法信息
                MethodBase method = frame.GetMethod();
                System.Collections.Generic.List<string> list = method.GetParameters().ToList().Select(x => x.ToString()).ToList();
                string pars = "";
                for (int i = 0; i < list.Count; i++)
                {
                    if (i != 0)
                    {
                        pars += ",";
                    }
                    pars += list[i];
                }
                string msg = method.DeclaringType.FullName + "."+ method.Name+"("+ pars + ")行"+v;
                vs.Add(msg);
            }
/*            Debug.Print("Start ：" + a + "\t" + b);
            a = 200;
            b = 400;*/
        }

        public static void After(int result)
        {
            Debug.Print("End ：" + result);
        }
    }
}
